﻿using System;
using Business;
using Entities;
using Common.Constants;
using System.Globalization;
namespace ServicesDirectory.UserControl
{
    public partial class ManagerContact : System.Web.UI.UserControl
    {
        public event EventHandler CloseControl;
        public int ActionForm { get; set; }
        public string ContactId { get; set; }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {

            cboContactType.DataSource = ContactBusiness.Instance.GetContactType();
            cboContactType.DataTextField = "DataRef";
            cboContactType.DataValueField = "RefValue";
            cboContactType.DataBind();
            cboBestContact.DataSource = ContactBusiness.Instance.BestContactMethod();
            cboBestContact.DataTextField = "DataRef";
            cboBestContact.DataValueField = "RefValue";
            cboBestContact.DataBind();

            if (ViewState["actionForm"] != null && int.Parse(ViewState["actionForm"].ToString(), CultureInfo.InvariantCulture) == (int)ModeForm.Update)
                lblTitleContact.Text = "Update contact";
            else
            {
                lblTitleContact.Text = "Add new contact";

            }
            LookupContactManager1.GetManager += LookupContactManager1_GetManager;
            LookupContactManager1.CloseControl += LookupContactManager1_CloseControl;
        }

        private void LookupContactManager1_CloseControl(object sender, EventArgs e)
        {
            lookupc.Visible = false;
        }

        /// <summary>
        /// Handles the GetManager event of the LookupContactManager1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LookupContactManager1_GetManager(object sender, EventArgs e)
        {
            string message;
            var contactManager = ContactBusiness.Instance.GetContact(sender.ToString(),
                                                                     out message);
            if (contactManager != null)
            {
                lblManagerName.Text = contactManager.FirstName + " " + contactManager.SurName;
                lookup.CommandArgument = contactManager.ContactId.ToString();

            }
            else
            {
                lblManagerName.Text = "";
                lookup.CommandArgument = Guid.Empty.ToString();
            }

        }

        /// <summary>
        /// Handles the Click event of the lookup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Lookup_Click(object sender, EventArgs e)
        {
            lookupc.Visible = true;


        }

        /// <summary>
        /// Handles the Click event of the cmdBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdBack_Click(object sender, EventArgs e)
        {
            if (ViewState["contactId"] != null)
                ContactId = ViewState["contactId"].ToString();
            string action = "";
            if (ViewState["actionForm"] != null)
                action = ViewState["actionForm"].ToString();
            if (CloseControl != null)
                CloseControl(action, e);

        }


        /// <summary>
        /// Binds the contact.
        /// </summary>
        public void BindContact()
        {
            lblTitleContact.Text = "Update contact";
            string message;
            var contact = ContactBusiness.Instance.GetContact(ContactId, out message);
            if (contact != null)
            {

                txtEmail.Text = contact.Email;
                txtFirstName.Text = contact.FirstName;
                txtJobRole.Text = contact.JobRole;
                txtJobTitle.Text = contact.JobTitle;
                txtKnownAs.Text = contact.KnownAs;
                txtMobilePhone.Text = contact.MobilePhone;
                txtOfficePhone.Text = contact.OfficePhone;
                txtStHomePhone.Text = contact.StHomePhone;
                txtSurname.Text = contact.SurName;
                txtWorkbase.Text = contact.Workbase;
                cboBestContact.SelectedValue = contact.BestContactType.ToString(CultureInfo.InvariantCulture);
                cboContactType.SelectedValue = contact.ContactType.ToString(CultureInfo.InvariantCulture);
                chkActive.Checked = contact.IsActive;
                if (contact.ManagerId.ToString() != string.Empty && contact.ManagerId != Guid.Empty)
                {
                    var contactManager = ContactBusiness.Instance.GetContact(contact.ManagerId.ToString(),
                                                                             out message);

                    if (contactManager != null)
                    {
                        lblManagerName.Text = contactManager.FirstName + " " + contactManager.SurName;
                    }
                }

                ViewState["contactId"] = ContactId;
            }
            lblannounce.Text = message;
            lblannounce.Visible = true;
        }
        public void SetModeForm(string modeForm)
        {
            ViewState["actionForm"] = modeForm;
        }

        /// <summary>
        /// Handles the Click event of the cmdSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdSave_Click(object sender, EventArgs e)
        {
            int actionForm;
            string action = "";
            if (ViewState["actionForm"]!=null&&int.TryParse(ViewState["actionForm"].ToString(), out actionForm))
            {

                if (actionForm == (int)ModeForm.Update)
                {

                    var contact = new Contact();
                    contact.ContactId = new Guid(ViewState["contactId"].ToString());
                    int bestContactMethod;
                    if (int.TryParse(cboBestContact.SelectedValue, out bestContactMethod))
                        contact.BestContactType = bestContactMethod;
                    int typeContact;
                    if (int.TryParse(cboContactType.SelectedValue, out typeContact))
                        contact.ContactType = typeContact;
                    contact.Email = txtEmail.Text;
                    contact.FirstName = txtFirstName.Text;
                    contact.IsActive = chkActive.Checked;
                    contact.JobRole = txtJobRole.Text;
                    contact.JobTitle = txtJobTitle.Text;
                    contact.KnownAs = txtKnownAs.Text;
                    if (!string.IsNullOrEmpty(lookup.CommandName))
                        contact.ManagerId = new Guid(lookup.CommandArgument);
                    contact.MobilePhone = txtMobilePhone.Text;
                    contact.OfficePhone = txtOfficePhone.Text;
                    contact.StHomePhone = txtStHomePhone.Text;
                    contact.SurName = txtSurname.Text;
                    contact.Workbase = txtWorkbase.Text;
                    var message = ContactBusiness.Instance.Update(contact);

                    lblannounce.Text = message;
                    lblannounce.Visible = true;
                    if (message == UIMessage.UpdateContactSuccess)
                    {
                        ContactId = contact.ContactId.ToString();
                    if (CloseControl != null)
                        CloseControl(((int)ModeForm.Update).ToString(), e);
                        
                    }

                }
                if (actionForm == (int)ModeForm.Add)
                {


                    var contact = new Contact();
                    contact.ContactId = Guid.NewGuid();
                    int bestContactMethod;
                    if (int.TryParse(cboBestContact.SelectedValue, out bestContactMethod))
                        contact.BestContactType = bestContactMethod;
                    int typeContact;
                    if (int.TryParse(cboContactType.SelectedValue, out typeContact))
                        contact.ContactType = typeContact;
                    contact.Email = txtEmail.Text;
                    contact.FirstName = txtFirstName.Text;
                    contact.IsActive = chkActive.Checked;
                    contact.JobRole = txtJobRole.Text;
                    contact.JobTitle = txtJobTitle.Text;
                    contact.KnownAs = txtKnownAs.Text;
                    if (!string.IsNullOrEmpty(lookup.CommandName))
                        contact.ManagerId = new Guid(lookup.CommandArgument);
                    contact.MobilePhone = txtMobilePhone.Text;
                    contact.OfficePhone = txtOfficePhone.Text;
                    contact.StHomePhone = txtStHomePhone.Text;
                    contact.SurName = txtSurname.Text;
                    contact.Workbase = txtWorkbase.Text;
                    var message = ContactBusiness.Instance.Insert(contact);
                    ViewState["contactId"] = contact.ContactId;
                    lblannounce.Text = message;
                    lblannounce.Visible = true;
                    if (message == UIMessage.InsertContactSuccess)
                    {
                        ContactId = contact.ContactId.ToString();
                        if (CloseControl != null)
                            CloseControl(((int)ModeForm.Add).ToString(), e);}

                }
            }
            else
            {



                var contact = new Contact();
                contact.ContactId = Guid.NewGuid();
                int bestContactMethod;
                if (int.TryParse(cboBestContact.SelectedValue, out bestContactMethod))
                    contact.BestContactType = bestContactMethod;
                int typeContact;
                if (int.TryParse(cboContactType.SelectedValue, out typeContact))
                    contact.ContactType = typeContact;
                contact.Email = txtEmail.Text;
                contact.FirstName = txtFirstName.Text;
                contact.IsActive = chkActive.Checked;
                contact.JobRole = txtJobRole.Text;
                contact.JobTitle = txtJobTitle.Text;
                contact.KnownAs = txtKnownAs.Text;
                if (!string.IsNullOrEmpty(lookup.CommandName))
                    contact.ManagerId = new Guid(lookup.CommandArgument);
                contact.MobilePhone = txtMobilePhone.Text;
                contact.OfficePhone = txtOfficePhone.Text;
                contact.StHomePhone = txtStHomePhone.Text;
                contact.SurName = txtSurname.Text;
                contact.Workbase = txtWorkbase.Text;
                var message = ContactBusiness.Instance.Insert(contact);
                ViewState["contactId"] = contact.ContactId;
                lblannounce.Text = message;
                lblannounce.Visible = true;
                if (message == UIMessage.InsertContactSuccess)
                    if (CloseControl != null)
                        CloseControl(((int)ModeForm.Add).ToString(), e);

            }

        }
    }
}